import React from 'react';
import { connect } from 'dva';
import { Divider } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';
import { urlParse } from '@/utils/utils';

@connect(({ addass, publicdata, loading }) => ({
  uploading: loading.effects['addass/uploadFileWithProgress'],
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  currentStep: addass.currentStep,
  progressPercent: addass.progressPercent,
  resultInfo: addass.resultInfo,
  showProgress: addass.showProgress,
}))
class Batch extends React.PureComponent {
  componentDidMount() {
    const { dispatch, location } = this.props;
    const { batchAdjType } = urlParse(location.search);
    dispatch({
      type: 'addass/updateQuery',
      payload: { batchAdjType, progressPercent: 0 },
    });
    dispatch({
      type: 'addass/getDownloadInfo',
      payload: { tempType: 55 },
    });
    this.clearModel();
  }

  componentWillUnmount() {
    this.clearModel();
  }

  // 重置model
  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'addass/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'addass/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      this.clearModel();
    }
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const { dispatch } = this.props;
    dispatch({
      type: 'addass/uploadFileWithProgress',
      payload: formData,
    })
  };

  render() {
    const isExactDiv = () => (
      <div>
        <Divider dashed style={{ marginTop: 100 }} />
        <p style={{ margin: '0 auto', width: '90%', fontSize: 14, textAlign: 'left' }}>
          注：
          <br />
          1、模板中每个险种的开始日期和申报工资必须同时填写，如果某个险种信息不填写，代表此险种不增加；开始日期必须填写每月1号；
          <br />
          2、添加的险种如果使用不同的缴费方式，需要填写服务配置；
        </p>
      </div>
    );
    const {
      fileId,
      fileName,
      uploading,
      currentStep,
      showProgress,
      resultInfo,
      progressPercent,
    } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <FileUpload
            title="上传人员"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: { fileId, fileName },
              url: '/csApi/batchTask/downloadFile',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            showProgress={showProgress}
            progressPercent={progressPercent || 0}
            progressTitle="批量增加险种任务"
            progressVisible={showProgress}
            isExact={isExactDiv}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
